package com.kcloud.commons.tools.codebuilder.impl;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kcloud.commons.authorization.annotation.AuthorizationAttr;
import com.kcloud.commons.authorization.annotation.AuthorizationGroup;
import com.kcloud.commons.entity.control.AttributeGroup;
import com.kcloud.commons.tools.codebuilder.CodeBuilder;
import com.kcloud.commons.tools.codebuilder.CodeBuilderConfig;
import com.kcloud.commons.tools.codebuilder.toolkit.DBTable;
import com.squareup.javapoet.*;
import lombok.AllArgsConstructor;
import lombok.Data;

import javax.lang.model.element.Modifier;
import javax.sql.DataSource;

/**
 * 权限对象生成器
 *
 * @author songhuiqing
 */
@Data
@AllArgsConstructor
public class AuthorizationEntityCodeBuilder implements CodeBuilder {

    private CodeBuilderConfig config;


    @Override
    public DataSource getDataSource() {
        return config.getDataSource();
    }

    public JavaFile generateJavaFile(DBTable table) {
        //生成一个java的实体对象
        String withoutPrefixTableName = table.getName();
        if (config.isIgnorePrefix() && withoutPrefixTableName.toLowerCase().contains(config.getPrefix().toLowerCase())) {
            withoutPrefixTableName = StringUtils.removePrefixAfterPrefixToLower(table.getName(), config.getPrefix().length());
        }
        String authName = StringUtils.underlineToCamel(withoutPrefixTableName);
        String entityName = StringUtils.capitalize(authName);

        // 属性集注解
        AnnotationSpec.Builder attrGrpBuilder = AnnotationSpec.builder(AuthorizationGroup.class)
                .addMember("code", "$S", authName)
                .addMember("name", "$S", table.getTableDesc())
                .addMember("type", "$S", table.getTableGroupType());
        //TODO 如果为子集的时候，获取主集的编码
        if(AttributeGroup.GROUP_TYPE_CHILD.equalsIgnoreCase(table.getTableGroupType())) {
            attrGrpBuilder.addMember("parent", "$S", "主集编码");
        }

        TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(entityName)
                .addModifiers(Modifier.PUBLIC)
                .addJavadoc(table.getTableDesc())
                .addAnnotation(Data.class)
//                .addAnnotation(AnnotationSpec.builder(Authorization.class).addMember("value", "$S", authName).build())
                .addAnnotation(attrGrpBuilder.build())
                .addAnnotation(AnnotationSpec.builder(TableName.class).addMember("value", "$S", table.getName()).build());
        table.getColumns().forEach(col -> {

            // 属性注解
            AnnotationSpec.Builder authAttrBuilder = AnnotationSpec.builder(AuthorizationAttr.class)
                    .addMember("name", "$S", col.getRemark())
                    .addMember("code", "$S", StringUtils.underlineToCamel(col.getName()));
            if (col.isPrimaryKey()) authAttrBuilder.addMember("primaryKey", "$L", true);
            if (col.isForeignKey()) authAttrBuilder.addMember("foreignKey", "$L", true);
            //属性
            FieldSpec fieldSpec = FieldSpec.builder(col.getType(), StringUtils.underlineToCamel(col.getName()))
                    .addModifiers(Modifier.PRIVATE)
                    .addJavadoc(col.getRemark())
                    .addAnnotation(authAttrBuilder.build())
                    .build();
            typeBuilder.addField(fieldSpec);
        });
        return JavaFile.builder(String.format("%s.%s", config.getPackagePath(), entityBasePackage), typeBuilder.build()).build();
    }
}
